/**
 * Mikael Nilsson
 * Johan Schedin Jigland
 * Nikolai Padyukov
 * Arkitektur och design av globala applikationer, IV1201
 * 
 * 2011-03-02
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

package org.kth.dbcopy.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.border.MatteBorder;

import org.kth.dbcopy.controller.Controller;
import org.kth.dbcopy.observer.TransferObserver;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * This is the main view of the program. It take the following input:
 * Old db username, password and jdbc-url.
 * new db username, password and jdbc-url.
 * 
 * It has one button which invokes the controller to start the data transfering process.
 * 
 * @author Mikael Nilsson
 */
public class View extends JFrame implements TransferObserver {
	private static final long serialVersionUID = 1L;
	private JTextField old_userName;
	private JTextField old_url;
	private JPasswordField old_password;
	private JTextField new_userName;
	private JPasswordField new_password;
	private JTextField new_url;
	private Controller ctrl;
	private JButton transferDataButton;
	private JPanel bottomPanel;
	private JProgressBar progress;
	
	/**
	 * The only constructor of the view.
	 * @param ctrl This views <class>Controller</class>.
	 */
	public View(Controller ctrl) {
		this.setTitle("IV1201 Database Copy");
		this.ctrl = ctrl;
		setResizable(false);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((int)(size.getWidth()-654)/2,(int)(size.getHeight()-316)/2,654,216);
		
		JPanel panel_9 = new JPanel();
		getContentPane().add(panel_9, BorderLayout.CENTER);
		panel_9.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_10 = new JPanel();
		panel_10.setBorder(new MatteBorder(0, 0, 1, 0, (Color) new Color(0, 0, 0)));
		panel_9.add(panel_10);
		panel_10.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_8 = new JPanel();
		panel_10.add(panel_8, BorderLayout.CENTER);
		panel_8.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel_8.add(panel, BorderLayout.NORTH);
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setVgap(0);
		flowLayout.setHgap(15);
		flowLayout.setAlignment(FlowLayout.LEFT);
		
		JLabel lblNewLabel = new JLabel("DB Username:");
		panel.add(lblNewLabel);
		
		old_userName = new JTextField();
		old_userName.setText("root");
		panel.add(old_userName);
		old_userName.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		panel_8.add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_2.getLayout();
		flowLayout_1.setVgap(0);
		flowLayout_1.setHgap(15);
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_1.add(panel_2, BorderLayout.NORTH);
		
		JLabel lblNewLabel_1 = new JLabel("DB Password:");
		lblNewLabel_1.setPreferredSize(new Dimension(87, 16));
		lblNewLabel_1.setMinimumSize(new Dimension(87, 16));
		lblNewLabel_1.setMaximumSize(new Dimension(87, 16));
		panel_2.add(lblNewLabel_1);
		
		old_password = new JPasswordField();
		old_password.setText("q1w2e3r4");
		old_password.setColumns(10);
		panel_2.add(old_password);
		
		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3, BorderLayout.CENTER);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panel_4.getLayout();
		flowLayout_2.setVgap(0);
		flowLayout_2.setHgap(15);
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_4, BorderLayout.NORTH);
		
		JLabel lblNewLabel_2 = new JLabel("URL:");
		panel_4.add(lblNewLabel_2);
		
		old_url = new JTextField();
		old_url.setText("jdbc:mysql://localhost/old_db");
		panel_4.add(old_url);
		old_url.setColumns(20);
		
		JPanel panel_12 = new JPanel();
		panel_10.add(panel_12, BorderLayout.NORTH);
		
		JLabel lblOldDb = new JLabel("Old DB");
		lblOldDb.setFont(new Font("Lucida Grande", Font.PLAIN, 22));
		panel_12.add(lblOldDb);
		
		JPanel panel_11 = new JPanel();
		panel_11.setBorder(new MatteBorder(0, 1, 1, 0, (Color) new Color(0, 0, 0)));
		panel_9.add(panel_11);
		panel_11.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_13 = new JPanel();
		panel_11.add(panel_13, BorderLayout.CENTER);
		panel_13.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_15 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_15.getLayout();
		flowLayout_3.setAlignment(FlowLayout.LEFT);
		flowLayout_3.setVgap(0);
		flowLayout_3.setHgap(15);
		panel_13.add(panel_15, BorderLayout.NORTH);
		
		JLabel label_1 = new JLabel("DB Username:");
		panel_15.add(label_1);
		
		new_userName = new JTextField();
		new_userName.setColumns(10);
		new_userName.setText("root");
		panel_15.add(new_userName);
		
		JPanel panel_16 = new JPanel();
		panel_13.add(panel_16, BorderLayout.CENTER);
		panel_16.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_17 = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) panel_17.getLayout();
		flowLayout_4.setAlignment(FlowLayout.LEFT);
		flowLayout_4.setVgap(0);
		flowLayout_4.setHgap(15);
		panel_16.add(panel_17, BorderLayout.NORTH);
		
		JLabel label_2 = new JLabel("DB Password:");
		label_2.setPreferredSize(new Dimension(87, 16));
		label_2.setMinimumSize(new Dimension(87, 16));
		label_2.setMaximumSize(new Dimension(87, 16));
		panel_17.add(label_2);
		
		new_password = new JPasswordField();
		new_password.setColumns(10);
		new_password.setText("q1w2e3r4");
		panel_17.add(new_password);
		
		JPanel panel_18 = new JPanel();
		panel_16.add(panel_18, BorderLayout.CENTER);
		panel_18.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_19 = new JPanel();
		FlowLayout flowLayout_5 = (FlowLayout) panel_19.getLayout();
		flowLayout_5.setAlignment(FlowLayout.LEFT);
		flowLayout_5.setVgap(0);
		flowLayout_5.setHgap(15);
		panel_18.add(panel_19, BorderLayout.NORTH);
		
		JLabel lblUrl = new JLabel("URL:");
		panel_19.add(lblUrl);
		
		new_url = new JTextField();
		new_url.setText("jdbc:mysql://localhost/iv1201");
		panel_19.add(new_url);
		new_url.setColumns(20);
		
		JPanel panel_14 = new JPanel();
		panel_11.add(panel_14, BorderLayout.NORTH);
		
		JLabel lblNewDb = new JLabel("New DB");
		lblNewDb.setFont(new Font("Lucida Grande", Font.PLAIN, 22));
		panel_14.add(lblNewDb);
		
		bottomPanel = new JPanel();
		getContentPane().add(bottomPanel, BorderLayout.SOUTH);
		
		transferDataButton = new JButton("Transfer Data");
		transferDataButton.addActionListener(transferData);
		bottomPanel.add(transferDataButton);
		
		setVisible(true);
	}
	
	
	private ActionListener transferData = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			progress = new JProgressBar();
			progress.setIndeterminate(true);
			
			bottomPanel.remove(transferDataButton);
			bottomPanel.add(progress);
			
			validate();

			ctrl.transferData(old_userName.getText(), old_password.getText(), old_url.getText(),
							new_userName.getText(), new_password.getText(), new_url.getText(), View.this);

		}
	};

	@Override
	public void done() {
		bottomPanel.remove(progress);
		bottomPanel.add(new JLabel("Transfer complete!"));
		validate();
	}

	@Override
	public void exception(String message) {
		JOptionPane.showMessageDialog(this, "Exception: " + message + ".\nTransfering of data failed.");
		
		bottomPanel.remove(progress);
		bottomPanel.add(transferDataButton);
		validate();
	}
}
